import request from '@/utils/request'

interface ProductQuery {
  pageCurrent: number
  pageSize: number
  categoryId?: number
}

interface CategoryQuery {
  name?: string
}

interface Product {
  id?: number
  name: string
  description: string
  mainImage: string
  images: string[]
  categoryId: number
  stock: number
  isPrescription: number
  status: number
  sort: number
}

interface Category {
  id?: number
  name: string
  imageUrl: string
  sort: number
  isHot: number
  status: number
}

// 商品相关接口
export const getProductList = (params: ProductQuery) => {
  return request({
    url: '/app/product/list',
    method: 'get',
    params
  })
}

export const getProductDetail = (id: number) => {
  return request({
    url: '/app/product/detail',
    method: 'get',
    params: { productId: id }
  })
}

export const addProduct = (data: Product) => {
  return request({
    url: '/app/product',
    method: 'post',
    data
  })
}

export const updateProduct = (data: Product) => {
  return request({
    url: '/app/product',
    method: 'put',
    data
  })
}

export const deleteProduct = (id: number) => {
  return request({
    url: `/app/product/${id}`,
    method: 'delete'
  })
}

// 分类相关接口
export function listCategory(query?: CategoryQuery) {
  return request({
    url: '/app/product/categories',
    method: 'get',
    params: query
  })
}

export function getCategory(id: number) {
  return request({
    url: '/app/product/category/' + id,
    method: 'get'
  })
}

export function addCategory(data: Category) {
  return request({
    url: '/app/product/category',
    method: 'post',
    data: data
  })
}

export function updateCategory(data: Category) {
  return request({
    url: '/app/product/category',
    method: 'put',
    data: data
  })
}

export function delCategory(id: number) {
  return request({
    url: '/app/product/category/' + id,
    method: 'delete'
  })
} 